package com.pk.platform.business.question.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.business.data.service.IBaseDataService;
import com.pk.platform.business.question.service.IPaperDefineService;
import com.pk.platform.business.question.vo.PaperVO;
import com.pk.platform.domain.data.BaseData;
import com.pk.platform.domain.question.CodeTable;
import com.pk.platform.domain.question.ItemOption;
import com.pk.platform.domain.question.Paper;
import com.pk.platform.domain.question.Question;
import com.pk.platform.util.SelectOption;
import com.pk.platform.util.constant.QuestionConstant;

public class PaperDefineAction extends GenericAction {

	private IPaperDefineService paperDefineService;
	
	private PaperVO pvo = new PaperVO();
	
	private Paper paper = new Paper();
	
	private Question qes = new Question();
	
	private CodeTable ct = new CodeTable();
	
	private ItemOption io = new ItemOption();
	
	private List<BaseData> qesTypeList = new ArrayList<BaseData>();
	
	private List<SelectOption> commonList = new ArrayList<SelectOption>();
	
	private List<Map<String, Object>> ctList = new ArrayList<Map<String,Object>>();

	private List<Map<String, Object>> subQesList = new ArrayList<Map<String,Object>>();
	
	private IBaseDataService baseDataService;
	
	private Map<String, Object> returnObj = new HashMap<String, Object>();
	
	private String keyArray;

	private String labelArray;
	
	private String[] jqglist;
	
	public String paperDefineManage()
	{
		return SUCCESS;
	}
	
	public String paperDefineListByPage()
	{
		list = paperDefineService.queryPaperByPage(pvo, pager);
		return SUCCESS;
	}
	
	public String toAddPaper()
	{
		return SUCCESS;
	}
	
	public String addPaper()
	{
		message = paperDefineService.addPaper(paper);
		return SUCCESS;
	}
	
	public String toUpdatePaper()
	{
		paper = paperDefineService.queryPaperById(paper.getId());
		return SUCCESS;
	}

	public String updatePaper()
	{
		paperDefineService.updatePaper(paper);
		return SUCCESS;
	}
	
	public String delPaper()
	{
		message = paperDefineService.delPaper(paper.getId());
		return SUCCESS;
	}
	
	public String questionListByPage()
	{
		list = paperDefineService.queryQuestionByPage(pvo, pager);
		return SUCCESS;
	}
	
	public String toAddQuestion()
	{
		commonList = this.commonListMethod();
		qesTypeList = baseDataService.queryBaseDataByParentId(QuestionConstant.QUESTION_TYPE);
		ctList = paperDefineService.queryCodeTableByPaper(qes.getPaper().getId(), QuestionConstant.CT_TYPE_CODE_TABLE);
		subQesList = paperDefineService.queryCodeTableByPaper(qes.getPaper().getId(), QuestionConstant.CT_TYPE_GROUP_QES);
		return SUCCESS;
	}
	
	public String addQuestion()
	{
		returnObj = paperDefineService.addQuestion(qes);
		return SUCCESS;
	}
	
	public String toUpdateQuestion()
	{
		qes = paperDefineService.queryQuestionById(qes.getId());
		commonList = this.commonListMethod();
		qesTypeList = baseDataService.queryBaseDataByParentId(QuestionConstant.QUESTION_TYPE);
		ctList = paperDefineService.queryCodeTableByPaper(qes.getPaper().getId(), QuestionConstant.CT_TYPE_CODE_TABLE);
		subQesList = paperDefineService.queryCodeTableByPaper(qes.getPaper().getId(), QuestionConstant.CT_TYPE_GROUP_QES);
		return SUCCESS;
	}
	
	public String updateQuestion()
	{
		message = paperDefineService.updateQuestion(qes);
		return SUCCESS;
	}
	
	public String delQuestion()
	{
		paperDefineService.delQuestion(qes.getId());
		return SUCCESS;
	}
	
	public String toAddCodeTable()
	{
		commonList = this.commonListMethod();
		return SUCCESS;
	}
	
	public String addCodeTable()
	{
		returnObj = paperDefineService.addCodeTable(ct);
		return SUCCESS;
	}
	
	public String toUpdateCodeTable()
	{
		commonList = this.commonListMethod();
		ct = paperDefineService.queryCodeTableById(ct.getId());
		return SUCCESS;
	}
	
	public String updateCodeTable()
	{
		returnObj = paperDefineService.updateCodeTable(ct);
		return SUCCESS;
	}

	public String delCodeTable()
	{
		returnObj = paperDefineService.delCodeTable(ct.getId());
		return SUCCESS;
	}
	
	public String addItemOption()
	{
		paperDefineService.addItemOption(keyArray, labelArray, ct.getId());
		return SUCCESS;
	}
	
	public String delItemOption()
	{
		if(jqglist != null)
		{
			paperDefineService.delItemOption(jqglist);
		}
		return SUCCESS;
	}
	
	public String itemOptionList()
	{
		list = paperDefineService.queryItemOptionByCodeTable(ct.getId());
		return SUCCESS;
	}
	
	public String toUpdateItemOption()
	{
		io = paperDefineService.queryItemOptionById(io.getId());
		return SUCCESS;
	}
	
	public String updateItemOption()
	{
		paperDefineService.updateItemOption(io);
		return SUCCESS;
	}
	
	public String exportPaperMap()
	{
		Map<String, String> map = paperDefineService.exportPaperMap(paper.getId(), super.getRequest());
		String filePath = map.get("filePath");
		String name = map.get("name");
		super.toDownload(filePath, name);
		return SUCCESS;
	}
	
	
	private List<SelectOption> commonListMethod()
	{
		List<SelectOption> soList = new ArrayList<SelectOption>();
		for(int i = 1; i <= 15; i++)
		{
			SelectOption so = new SelectOption(i + "", i + "");
			soList.add(so);
		}
		return soList;
	}
	
	
	
	

	public IPaperDefineService getPaperDefineService() {
		return paperDefineService;
	}

	public void setPaperDefineService(IPaperDefineService paperDefineService) {
		this.paperDefineService = paperDefineService;
	}

	public PaperVO getPvo() {
		return pvo;
	}

	public void setPvo(PaperVO pvo) {
		this.pvo = pvo;
	}

	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public Question getQes() {
		return qes;
	}

	public void setQes(Question qes) {
		this.qes = qes;
	}

	public CodeTable getCt() {
		return ct;
	}

	public void setCt(CodeTable ct) {
		this.ct = ct;
	}

	public ItemOption getIo() {
		return io;
	}

	public void setIo(ItemOption io) {
		this.io = io;
	}

	public List<BaseData> getQesTypeList() {
		return qesTypeList;
	}

	public void setQesTypeList(List<BaseData> qesTypeList) {
		this.qesTypeList = qesTypeList;
	}

	public List<SelectOption> getCommonList() {
		return commonList;
	}

	public void setCommonList(List<SelectOption> commonList) {
		this.commonList = commonList;
	}

	public List<Map<String, Object>> getCtList() {
		return ctList;
	}

	public void setCtList(List<Map<String, Object>> ctList) {
		this.ctList = ctList;
	}

	public IBaseDataService getBaseDataService() {
		return baseDataService;
	}

	public void setBaseDataService(IBaseDataService baseDataService) {
		this.baseDataService = baseDataService;
	}

	public Map<String, Object> getReturnObj() {
		return returnObj;
	}

	public void setReturnObj(Map<String, Object> returnObj) {
		this.returnObj = returnObj;
	}

	public String getKeyArray() {
		return keyArray;
	}

	public void setKeyArray(String keyArray) {
		this.keyArray = keyArray;
	}

	public String getLabelArray() {
		return labelArray;
	}

	public void setLabelArray(String labelArray) {
		this.labelArray = labelArray;
	}

	public String[] getJqglist() {
		return jqglist;
	}

	public void setJqglist(String[] jqglist) {
		this.jqglist = jqglist;
	}

	public List<Map<String, Object>> getSubQesList() {
		return subQesList;
	}

	public void setSubQesList(List<Map<String, Object>> subQesList) {
		this.subQesList = subQesList;
	}

}
